﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Forecast.Domain.Implementation
{
    public class Holding : ICountStocks
    {
        #region Variables

        public static Holding NULL = new Holding(Prediction.NULL);
        private IPredict _prediction;
        private int _numStocks;
        private int _numForSale;

        #endregion
        #region Constructors

        public Holding(IPredict prediction)
        {
            if (prediction == null)
                throw new ArgumentNullException("prediction");
            
            _prediction = prediction;
        }

        #endregion
        #region Interface

        public void AddStocks(int numStocks)
        {
            if (numStocks < 0)
                throw new ArgumentException("Can't add a negative number", "numStocks");

            _numStocks += numStocks;
        }

        public void RemoveStocks(int numStocks)
        {
            if (numStocks < 0)
                throw new ArgumentException("Can't remove a negative number", "numStocks");
            if (numStocks > _numStocks)
                throw new ApplicationException(String.Format(
                    "Tried to sell too many stocks. Had {0}, but tried to sell {1}.",
                    _numStocks,
                    numStocks));

            _numStocks -= numStocks;
            _numForSale -= numStocks;
        }

        public void PutUpForSale(int numStocks)
        {
            if (numStocks == 0)
                throw new ArgumentException("Can't put no stocks up for sale.", "numStocks");
            if (numStocks < 0)
                throw new ArgumentException("Can't put up a negative number of stocks for sale.");
            if ((_numForSale + numStocks) > _numStocks)
                throw new ApplicationException(String.Format("Tried to sell {0}, but only has {1} left to sell.",
                    numStocks,
                    _numForSale));

            _numForSale += numStocks;
        }

        public void CancelSale(int numStocks)
        {
            if (numStocks < 0)
                throw new ArgumentException("Can't cancel a sale for a negative number of stocks.");
            if (numStocks > _numForSale)
                throw new ApplicationException(
                    String.Format("Tried to cancel the sale of {0} stocks, but only has {1} stocks for sale.",
                    numStocks,
                    _numForSale));

            _numForSale -= numStocks;
        }

        #endregion
        #region Properties

        public int NumStocks 
        {
            get { return _numStocks; }
        }
        
        public int NumForSale
        {
            get { return _numForSale; }
        }

        public int NumNotForSale
        {
            get { return NumStocks - NumForSale; }
        }

        public IPredict ThePrediction
        {
            get { return _prediction; }
        }

        #endregion
    }
}
